package com.bridge.client.bridgedynamcclient.controller;

import com.bridge.client.bridgedynamcclient.bean.vo.resp.BridgeSampleData;
import com.bridge.client.bridgedynamcclient.custom_compinents.SampleSelectItem;
import com.bridge.client.bridgedynamcclient.utils.FXMLUtil;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.util.Callback;

import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

public class BridgeSampleSelectController extends BaseController implements Initializable {


    @FXML
    private ListView<BridgeSampleData.BridgeSample> lvSampleSelect;

    @FXML
    private Button btnConfirm;

    private List<Long> selectIds=new ArrayList<>();
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        logger.info("initialize--");

        initListener();
    }


    private void initListener() {
        btnConfirm.setOnAction(a->{
            List<Long> ids = lvSampleSelect.getItems().stream().filter(item -> item.isSelect()).map(BridgeSampleData.BridgeSample::getId).toList();
            bridgeAddMainController.onSampleSelect(ids);
            FXMLUtil.closeState(btnConfirm.getScene());
        });
        lvSampleSelect.setCellFactory(new Callback<ListView<BridgeSampleData.BridgeSample>, ListCell<BridgeSampleData.BridgeSample>>() {
            @Override
            public ListCell<BridgeSampleData.BridgeSample> call(ListView<BridgeSampleData.BridgeSample> bridgeSampleListView) {
                return new ListCell<>(){
                    private Map<Long, SampleSelectItem> cacheItem = new HashMap<>();
                    @Override
                    protected void updateItem(BridgeSampleData.BridgeSample item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty || item == null) {
                            setText(null);
                            setGraphic(null);
                        } else {
                            SampleSelectItem sampleSelectItem = cacheItem.get(item.getId());
                            if (sampleSelectItem == null) {
                                 sampleSelectItem=new SampleSelectItem(item,selectIds.contains(item.getId()));
                                cacheItem.put(item.getId(), sampleSelectItem);
                                System.out.println("updateItem 没有缓存。创建view= " + sampleSelectItem.getId());
                            } else {
                                System.out.println("updateItem 有缓存。无需创建view= " + sampleSelectItem.getId());
                            }
                            setGraphic(sampleSelectItem);
                        }
                    }
                };
            }
        });
        lvSampleSelect.setItems(FXCollections.observableArrayList());

    }

    BridgeAddMainController bridgeAddMainController;
    public void setConfig(List<BridgeSampleData.BridgeSample> lists,List<Long> selectIds,BridgeAddMainController bridgeAddMainController) {
        logger.info("setConfig");
        this.bridgeAddMainController=bridgeAddMainController;
        if (lists.isEmpty()) {
            return;
        }
        this.selectIds.clear();
        this.selectIds.addAll(selectIds);
        lvSampleSelect.getItems().addAll(lists);
    }
}
